/*This script generates the figures and tables which draw on indivual wind turbine data found in the main text and appendix;
Figure 6b

Figure E1
Table E7



*/


cd "$data"
set more off

*bring in turbine data
clear
use turbine_JAERE.dta, replace



/*Figure 6b*/
preserve
replace t_cap =. if t_cap==-9999
collapse (sum) t_cap (firstnm) local area , by(p_year buffer_zone)

drop if p_year==.
drop if p_year==-9999
xtset local p_year
reshape wide t_cap, i(local) j(p_year)

reshape long t_cap, i(local) j(year)
xtset local year


replace t_cap=0 if t_cap==.
gen cumt_cap=t_cap if year==1998
replace cumt_cap=t_cap+l.cumt_cap if year>1998

gen cumat_c=cumt_/area

label var year "Year"


twoway (connected cumat_c year if local==0, msymbol(triangle) mcolor("128 244 255") lcolor("128 244 255") mlcolor(gs8) ) ///
(connected cumat_c year if local==1, msymbol(S) mcolor("7 81 208") lcolor("7 81 208")) ///
(connected cumat_c year if local==2, mcolor("1 35 109") lcolor("1 35 109")), ///
 scheme(s1color) legend(label(1 "Beyond Ogallala") label(2 "Border Region") label(3 "Central Ogallala") cols(3) region(lstyle(none))) plotregion(lstyle(none)) ytitle("Installed kW per Acre")	
	graph export  "$results/figures/fig6b_a_cap_buffer.pdf", replace
	
restore	

*Figure E1*/
*Note, the birdseye illustration is added ex post using a adobe acrobat pro, pasting in the elements provided in the excel spreadsheet fig3_fig5_figD2_CPISlayouts, tab figE1_support

twoway (kdensity centroid_dist if CPIS_bin<2, lc(black)) ///
(kdensity centroid_dist if CPIS_bin>1 & CPIS_bin<4, lc(green)  lpattern(dash) lw(medthick)) ///
(kdensity centroid_dist if CPIS_bin>=4, lc(midblue)  lpattern(dash_dot) lw(medthick)) ///
 if centroid_dist<3733, xline(546, lcolor(red) lp(shortdash)) xline(1320, lp(shortdash) lc(blue)) xline(2951, lp(shortdash) lcolor(purple)) legend(position(2) label(1 "CPIS {&le} 1 quarter") label(2 "1 quarter < CPIS {&le} 3 quarters") label(3 "CPIS > 3 quarter")) ytitle("") xtitle("Turbine Distance to Section Centroid (ft)") ///
			text(.0004 546 "Inside corners")  ///
			text(.00062 1320 "Edge corners")  ///
			text(.00062 2951 "Outside corners")  ///
			text(15 2951 "6.3", placement(W)) 	
							graph export  "$results/figures/figE1_turbine_centroid_distance_distribution.pdf", replace


/*Table E7*/

local i=1
foreach y of varlist t_cap t_hh t_rs  {
	foreach x of varlist cpis_miles close {
		qui reghdfe `y' `x' xlong ylat if buffer_zone!="" & `y'>0 & p_year>0, absorb(p_year) cluster(cluster_km)
			estadd scalar Nclus =  e(N_clust)
			qui sum `y' if e(sample) ==1 
			local mdv = r(mean)
			estadd scalar MDV = `mdv'
	
			estimates store cpis_`x'_`i'
	
		local i=`i'+1
	
		qui reghdfe `y' `x' xlong ylat if buffer_zone!="" & `y'>0 & p_year>0, absorb(t_fip fip_name p_year) cluster(cluster_km)
			estadd scalar Nclus =  e(N_clust)
			qui sum `y' if e(sample) ==1 
			local mdv = r(mean)
			estadd scalar MDV = `mdv'

		estimates store cpis_`x'_`i'

		local i=`i'+1
		}
	}

*note, panels are exported separately
esttab cpis_cpis_miles_* using  "$results/tables/tabE7a_turbine_cpis.csv",  label  keep(cpis_miles ) star(* 0.1 ** 0.05 *** 0.01)  ar2 se(a3) b(a3) scalars("MDV Mean DV" "df_a_initial No. Fixed Effects" "Nclus N Clusters") replace

esttab cpis_close_* using "$results/tables/tabE7b_turbine_cpis.csv",  label  keep( close) star(* 0.1 ** 0.05 *** 0.01)  ar2 se(a3) b(a3) scalars("MDV Mean DV" "df_a_initial No. Fixed Effects"  "Nclus N Clusters") replace
	